package com.atguigu.tingshu.user.service.impl;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.common.util.MongoUtil;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.atguigu.tingshu.vo.user.LastUserListenPlayVo;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.atguigu.tingshu.common.util.MongoUtil.MongoCollectionEnum.USER_LISTEN_PROCESS;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserListenProcessServiceImpl implements UserListenProcessService {

	@Autowired
	private MongoTemplate mongoTemplate;

	@Autowired
	StringRedisTemplate stringRedisTemplate;

	@Autowired
	KafkaService kafkaService;

	@Override
	public void updateListenProcess(UserListenProcessVo userListenProcessVo) {
		String collectionName = MongoUtil.getCollectionName(USER_LISTEN_PROCESS, AuthContextHolder.getUserId());
		Criteria criteria = Criteria.where("userId").is(AuthContextHolder.getUserId()).and("albumId").is(userListenProcessVo.getAlbumId()).and("trackId").is(userListenProcessVo.getTrackId());
		Query query = Query.query(criteria);
		UserListenProcess userListenProcess = mongoTemplate.findOne(query, UserListenProcess.class, collectionName);
		if (userListenProcess == null) {
			userListenProcess = new UserListenProcess();
			userListenProcess.setId(String.valueOf(new Date().getTime()));
			userListenProcess.setUserId(AuthContextHolder.getUserId());
			userListenProcess.setAlbumId(userListenProcessVo.getAlbumId());
			userListenProcess.setTrackId(userListenProcessVo.getTrackId());
			userListenProcess.setBreakSecond(userListenProcessVo.getBreakSecond());
			userListenProcess.setIsShow(1);
			userListenProcess.setCreateTime(new Date());
			userListenProcess.setUpdateTime(new Date());
			mongoTemplate.save(userListenProcess,collectionName);
		}else {
			userListenProcess.setBreakSecond(userListenProcessVo.getBreakSecond());
			userListenProcess.setUpdateTime(new Date());
			mongoTemplate.save(userListenProcess,collectionName);
		}

		String redisKey = "track:play: "+ userListenProcessVo.getTrackId() +":"+ DateUtil.format(new Date(), "yyyy-MM-dd") + AuthContextHolder.getUserId();
		if (!Boolean.TRUE.equals(stringRedisTemplate.hasKey(redisKey))) {
			TrackStatMqVo trackStatMqVo = new TrackStatMqVo();
			trackStatMqVo.setBusinessNo(new Date().toString());
			trackStatMqVo.setAlbumId(userListenProcessVo.getAlbumId());
			trackStatMqVo.setTrackId(userListenProcessVo.getTrackId());
			trackStatMqVo.setStatType(SystemConstant.TRACK_STAT_PLAY);
			trackStatMqVo.setCount(1);
			kafkaService.sendMsg(KafkaConstant.QUEUE_TRACK_STAT_UPDATE, String.valueOf(trackStatMqVo));
			stringRedisTemplate.opsForValue().set(redisKey, JSON.toJSONString(userListenProcess),7, TimeUnit.DAYS);
		}
	}

	@Override
	public BigDecimal getTrackBreakSecond(Long trackId) {
		Criteria criteria = Criteria.where("userId").is(AuthContextHolder.getUserId()).and("trackId").is(trackId);
		String collectionName = MongoUtil.getCollectionName(USER_LISTEN_PROCESS, AuthContextHolder.getUserId());
		UserListenProcess userListenProcess = mongoTemplate.findOne(Query.query(criteria), UserListenProcess.class, collectionName);
		if (userListenProcess.getBreakSecond() == null) {
			return new BigDecimal(0);
		}
		return	userListenProcess.getBreakSecond();
	}

	@Override
	public LastUserListenPlayVo getLatelyTrack() {
		Criteria criteria = Criteria.where("userId").is(AuthContextHolder.getUserId());
		String collectionName = MongoUtil.getCollectionName(USER_LISTEN_PROCESS, AuthContextHolder.getUserId());
		UserListenProcess userListenProcess = mongoTemplate.findOne(Query.query(criteria).with(Sort.by(Sort.Direction.DESC, "updateTime")).limit(1), UserListenProcess.class, collectionName);
		LastUserListenPlayVo lastUserListenPlayVo = new LastUserListenPlayVo();
		if (userListenProcess == null) {
			lastUserListenPlayVo.setAlbumId(1429L);
			lastUserListenPlayVo.setTraceId(48237L);
		}else {
			lastUserListenPlayVo.setAlbumId(userListenProcess.getAlbumId());
			lastUserListenPlayVo.setTraceId(userListenProcess.getTrackId());
		}
		return lastUserListenPlayVo;
	}
}
